package com.system.seckillgoods.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.bean.SeckillResult;
import com.system.common.service.ISeckillResultService;
import com.system.common.util.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/seckillresult")
public class SeckillResultController {

    @DubboReference
    private ISeckillResultService seckillResultService;

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        return Result.ok("查询成功", seckillResultService.getSeckillResultById(id));
    }

    @GetMapping("/getByUserAndSeckill")
    public Result getByUserAndSeckill(@RequestParam Integer userId,
                                      @RequestParam Integer seckillId,
                                      @RequestParam Integer activityId) {
        return Result.ok("查询成功", seckillResultService.getSeckillResultByUserAndSeckill(userId, seckillId, activityId));
    }

    @PostMapping
    public Result add(@RequestBody SeckillResult seckillResult) {
        seckillResultService.addSeckillResult(seckillResult);
        return Result.ok("新增秒杀结果成功");
    }

    @PutMapping
    public Result update(@RequestBody SeckillResult seckillResult) {
        seckillResultService.updateSeckillResult(seckillResult);
        return Result.ok("更新秒杀结果成功");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        seckillResultService.deleteSeckillResult(id);
        return Result.ok("删除秒杀结果成功");
    }

    @GetMapping("/list")
    public Result list() {
        return Result.ok("查询秒杀结果列表成功", seckillResultService.getSeckillResultList());
    }

    @GetMapping("/page")
    public Result page(SeckillResult seckillResult,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.ok("分页查询秒杀结果成功",
                seckillResultService.getPage(seckillResult, new Page<>(pageNum, pageSize)));
    }

    @GetMapping("/hasSeckilled")
    public Result hasSeckilled(@RequestParam Integer userId,
                               @RequestParam Integer seckillId,
                               @RequestParam Integer activityId) {
        boolean exist = seckillResultService.hasUserSeckilled(userId, seckillId, activityId);
        return Result.ok("查询成功", exist);
    }
}
